package protectjava;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/*Session 跟踪实例
本实例说明了如何使用 HttpSession 对象获取
 session 会话创建时间和最后访问时间。
 如果不存在 session 会话，我们将通过请求创建一个新的
  session 会话。*/
@WebServlet(name = "SessionTrack")
public class SessionTrack extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        如果不存在session会话，则创建一个新的session对象
        HttpSession session = request.getSession(true);
//        获取session创建的时间
        Date createTime = new Date(session.getCreationTime());
//        获取该网页最后一次访问时间
        Date lastAccessTime = new Date(session.getLastAccessedTime());

//        设置日期输出的格式
        SimpleDateFormat df = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        String title = "Servlet session 实例";
        Integer visitCount = new Integer(0);
        String visitCountKey = new String("visitCount");
        String userIDKey = new String("userID");
        String userID = new String("runoob");
        if (session.getAttribute(visitCountKey) == null){
            session.setAttribute(visitCountKey, new Integer(0));
        }

//        检查网页上是否有新的访问者
        if (session.isNew()){
            title = "servlet session 实例";
            session.setAttribute(userIDKey,userID);
        }else{
            visitCount = (Integer)session.getAttribute(visitCountKey);
            visitCount = visitCount + 1;
            userID = (String)session.getAttribute(userIDKey);
        }
        session.setAttribute(visitCountKey, visitCountKey);

//        设置响应内容类型
        response.setContentType("text/heml:charset=UTF-8");
        PrintWriter out = response.getWriter();

        String docType = "<!DOCTYPE html>\n";
        out.print(docType+
                "                \"<html>\\n\" +\n" +
                "                \"<head><title>\" + title + \"</title></head>\\n\" +\n" +
                "                \"<body bgcolor=\\\"#f0f0f0\\\">\\n\" +\n" +
                "                \"<h1 align=\\\"center\\\">\" + title + \"</h1>\\n\" +\n" +
                "                 \"<h2 align=\\\"center\\\">Session 信息</h2>\\n\" +\n" +
                "                \"<table border=\\\"1\\\" align=\\\"center\\\">\\n\" +\n" +
                "                \"<tr bgcolor=\\\"#949494\\\">\\n\" +\n" +
                "                \"  <th>Session 信息</th><th>值</th></tr>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"  <td>id</td>\\n\" +\n" +
                "                \"  <td>\" + session.getId() + \"</td></tr>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"  <td>创建时间</td>\\n\" +\n" +
                "                \"  <td>\" +  df.format(createTime) + \n" +
                "                \"  </td></tr>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"  <td>最后访问时间</td>\\n\" +\n" +
                "                \"  <td>\" + df.format(lastAccessTime) + \n" +
                "                \"  </td></tr>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"  <td>用户 ID</td>\\n\" +\n" +
                "                \"  <td>\" + userID + \n" +
                "                \"  </td></tr>\\n\" +\n" +
                "                \"<tr>\\n\" +\n" +
                "                \"  <td>访问统计：</td>\\n\" +\n" +
                "                \"  <td>\" + visitCount + \"</td></tr>\\n\" +\n" +
                "                \"</table>\\n\" +\n" +
                "                \"</body></html>\"); "
        );
    }
}
